/*
 * Copyright (C) 2010 The Android Open Source Project 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

package org.lhlzi.ithink.compat;

import org.lhlzi.ithink.model.RecentContact;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.provider.Contacts.Phones;
import android.util.Log;

@SuppressWarnings("deprecation")
public class PeopleAccessor extends AbstractContactAccessor {

	public PeopleAccessor() {
		contentUri = People.CONTENT_URI;
		displayNameColumn = Phones.DISPLAY_NAME;
		personIdColumn = Phones.PERSON_ID;
		displayLabelColumn = Phones.TYPE;
		customDisplayLabelColumn = Phones.LABEL;
	}

	@Override
	public RecentContact getContactCursorBySearch(Context context,
			RecentContact recentContact) {

		ContentResolver resolver = context.getContentResolver();
		Cursor lookupCursor = null;

		if (recentContact.getNumber() != null
				&& recentContact.getPerson() != null) {

			lookupCursor = resolver.query(Contacts.Phones.CONTENT_URI,
					new String[] { personIdColumn, displayNameColumn, displayLabelColumn,  customDisplayLabelColumn},
					Phones.NUMBER + " = ? OR " + displayNameColumn + " = ?",
					new String[] { recentContact.getNumber(),
							recentContact.getPerson() }, null);

		} else if (recentContact.getPerson() != null) {

			// Search by contact name

			lookupCursor = resolver.query(Contacts.Phones.CONTENT_URI,
					new String[] { personIdColumn, displayNameColumn, displayLabelColumn,  customDisplayLabelColumn },
					displayNameColumn + " = ?", new String[] { recentContact
							.getPerson() }, null);

		} else {

			// Search by number by default

			lookupCursor = resolver.query(Contacts.Phones.CONTENT_URI,
					new String[] { personIdColumn, displayNameColumn, displayLabelColumn,  customDisplayLabelColumn},
					Phones.NUMBER + " = ?", new String[] { recentContact
							.getNumber() }, null);
		}

		if (lookupCursor != null && lookupCursor.getCount() >= 1) {
			try {
				if (lookupCursor.moveToFirst()) {

					initContactFromCursor(recentContact, lookupCursor, context);

					return recentContact;

				}
			} finally {
				lookupCursor.close();
			}
		}

		Log.d(TAG, "No contact found for " + recentContact);

		return recentContact;
	}

	@Override
	public RecentContact getContactCursorById(Context context,
			RecentContact recentContact) {

		ContentResolver resolver = context.getContentResolver();

		Cursor cursor = resolver.query(Contacts.Phones.CONTENT_URI,
				new String[] { personIdColumn, displayNameColumn, displayLabelColumn,  customDisplayLabelColumn },
				Phones.NUMBER + " = ?", new String[] { recentContact
						.getNumber() }, null);

		initContactFromCursor(recentContact, cursor, context);

		return recentContact;
	}

//	@Override
//	public ImageView createPopupBadge(Context context,
//			RecentContact recentContact) {
//
//		ImageButton badge = new ImageButton(context);
//		badge.setScaleType(ScaleType.FIT_CENTER);
//
//		// onClick can only be set by the client...
//		return badge;
//	}

	@Override
	public Bitmap loadContactPhoto(Context context, RecentContact recentContact) {

		/*
		 * Does not work either, returned stream is apparently not null.
		InputStream contactPhotoInputStream = People
				.openContactPhotoInputStream(context.getContentResolver(),
						ContentUris.withAppendedId(People.CONTENT_URI,
								recentContact.getPersonId()));

		if (contactPhotoInputStream != null) {
			return BitmapFactory.decodeStream(contactPhotoInputStream);
		}

		*/

		return People.loadContactPhoto(context, ContentUris.withAppendedId(
				People.CONTENT_URI, recentContact.getPersonId()),
				0, null);
	}

	protected void initContactFromCursor(RecentContact recentContact,
			Cursor lookupCursor, Context context) {

		recentContact.setPerson(lookupCursor.getString(lookupCursor
				.getColumnIndex(displayNameColumn)));
		
		
		
//		recentContact.setDisplayLabel(lookupCursor.getString(lookupCursor
//				.getColumnIndex(numberLabelColumn)));
		
		int labelId = lookupCursor.getInt(lookupCursor
				.getColumnIndex(displayLabelColumn));
		
		String label = null;
		
		if (labelId == 0) { // 自定义LABEL
			label = lookupCursor.getString(lookupCursor
					.getColumnIndex(customDisplayLabelColumn));
		} else if (labelId > 0) { // 系统LABEL
			label = Phones.getDisplayLabel(context, labelId, null).toString();
		}
		
		recentContact.setDisplayLabel(label);

		String personIdAsString = lookupCursor.getString(lookupCursor
				.getColumnIndex(personIdColumn));

		recentContact.setPersonId(Long.parseLong(personIdAsString));
	}

}
